import requests
import scrapy
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
from scrapy.utils.response import get_base_url


class MyspiderSpider(scrapy.Spider):
    name = "myspider"
    start_urls = ['https://www.toutiao.com/article/7309281289377563199/?log_from=57d0ff8f784db_1702022002681']

    def parse(self, response):
        print(response.text)

        # 使用 Selector 来解析 HTML 并获取 CSS 文件的 URL
        css_urls = response.css('link[rel="stylesheet"]::attr(href)').getall()
        print('=================')
        print(css_urls)
        for url in css_urls:
            print(url)
            # 构造完整的 CSS 文件 URL
            css_url = get_base_url(response) + url
            # 使用 requests 来下载 CSS 文件
            r = requests.get(css_url)
            with open(f'{url.split("/")[-1]}', 'wb') as f:
                f.write(r.content)
